---
title: "Replication Code for Partisan Hacks? How Election Cases Divide the Supreme Court"
author: "Albert H. Rivero^[Assistant Professor of Politics and John L. Nau III Assistant Professor of the History and Principles of Democracy, University of Virginia.]"
date: ""
output: 
  pdf_document:
    fig_caption: yes
    fig_crop: false
    citation_package: natbib
    keep_tex: false
    number_sections: true
fontsize: 12pt
documentclass: article
clean: true
---

```{r, include = FALSE}

library(broom)
library(rms)
library(tidyverse)
library(clubSandwich)
library(lmtest)
library(ggeffects)
library(stargazer)
library(ggpubr)
library(lubridate)
library(xtable)
library(lfe)
library(patchwork)


scdb <- read_rds("rivero_partisan_replication.rds")

# Election law cases and total cases
scdb %>% group_by(caseId) %>% slice(1) %>% pull(treatment) %>% sum()
scdb %>% group_by(caseId) %>% slice(1) %>% pull(treatment) %>% length() 

scdb_warren <- scdb %>% filter(chief == "Warren", ! is.na(early.salience.est))
scdb_burger <- scdb %>% filter(chief == "Burger", ! is.na(early.salience.est))
scdb_rehnquist <- scdb %>% filter(chief == "Rehnquist", ! is.na(early.salience.est))
scdb_roberts <- scdb %>% filter(chief == "Roberts")




# For comparing ways of measuring partisanship

party_1 <- lm(conforming_vote ~ treatment, data = scdb %>% mutate(conforming_vote = ifelse(conservative_vote == republican_pres, 1, 0))) %>%
  coeftest(vcov. = vcovCR, cluster = scdb$caseId, type = "CR1S") %>%
  tidy(conf.int = TRUE) %>% 
  filter(term == "(Intercept)") %>%
  mutate(Measurement = "Appointing President")
  
party_2 <- lm(conforming_vote ~ treatment, data = scdb %>% mutate(conforming_vote = ifelse(conservative_vote == republican, 1, 0))) %>%
  coeftest(vcov. = vcovCR, cluster = scdb$caseId, type = "CR1S") %>%
  tidy(conf.int = TRUE) %>% 
  filter(term == "(Intercept)") %>%
  mutate(Measurement = "Self-Described")

party_3 <- lm(conforming_vote ~ treatment, data = scdb) %>%
  coeftest(vcov. = vcovCR, cluster = scdb$caseId, type = "CR1S") %>%
  tidy(conf.int = TRUE) %>% 
  filter(term == "(Intercept)") %>%
  mutate(Measurement = "Revealed Preference")


control_plot_tbl <- party_1 %>%
  bind_rows(party_2, party_3) %>%
  mutate(`Conforming Vote` = estimate,
         lower_est = conf.low,
         upper_est = conf.high,
         Measurement = factor(Measurement, levels = c("Appointing President",
                                                     "Self-Described",
                                                     "Revealed Preference")))

# lrm: issueArea factor
scdb <- scdb %>% mutate(issueArea = factor(issueArea))
scdb_warren <- scdb_warren %>% mutate(issueArea = factor(issueArea))
scdb_burger <- scdb_burger %>% mutate(issueArea = factor(issueArea))
scdb_rehnquist <- scdb_rehnquist %>% mutate(issueArea = factor(issueArea))
scdb_roberts <- scdb_roberts %>% mutate(issueArea = factor(issueArea))
```


```{r partisan_cues_differ, echo = FALSE, message = FALSE, results = "asis"}

diff_chief <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(chief = paste(chief, "Court"), Variable = factor(chief, levels = c("Warren Court", "Burger Court", "Rehnquist Court", "Roberts Court"))) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop)) %>% mutate(Variable = as.character(Variable))
diff_appeal <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup()  %>% mutate(Variable = on_appeal) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "On Appeal") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_conflict_case <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(Variable = conflict_case) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "Federal Court Conflict") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_polarized_issue <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(Variable = polarized_issue) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "Civil Rights/Civil Liberties") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_judicial_review <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(Variable = judicial_review) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "Judicial Review") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_lc_conservative <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(Variable = lc_conservative) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "Lower Court Conservative Ruling") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_lc_alignment <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(Variable = lc_alignment) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "Lower Court Alignment") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_lc_disagreement <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(Variable = lc_disagreement) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "Lower Court Disagreement") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_lc_vertical_dis <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(Variable = lc_vertical_dis) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "Lower Court Disagreement (Vertical)") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_us_as_party <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup()  %>% mutate(Variable = us_as_party) %>% group_by(treatment_fct) %>% count(Variable) %>% mutate(Prop = n / sum(n)) %>% select(-n) %>% filter(Variable == 1) %>% mutate(Variable = "U.S. as Party") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Prop))
diff_early.salience.est <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% mutate(Variable = early.salience.est) %>% group_by(treatment_fct) %>% summarize(Mean_Var = mean(Variable, na.rm = TRUE)) %>% mutate(Variable = "Early Salience Estimate") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(Mean_Var))
diff_obs <- scdb %>% count(treatment_fct) %>% mutate(Variable = "Number of Cases") %>% pivot_wider(names_from = c(treatment_fct), values_from = c(n))

case_level <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup()
p_values <- c(
  # Warren Court
   t.test(case_level %>% filter(treatment == 0) %>% mutate(is_warren = ifelse(chief == "Warren", 1, 0)) %>% pull(is_warren),
          case_level %>% filter(treatment == 1) %>% mutate(is_warren = ifelse(chief == "Warren", 1, 0)) %>% pull(is_warren))$p.value,
   # Burger Court
      t.test(case_level %>% filter(treatment == 0) %>% mutate(is_burger = ifelse(chief == "Burger", 1, 0)) %>% pull(is_burger),
          case_level %>% filter(treatment == 1) %>% mutate(is_burger = ifelse(chief == "Burger", 1, 0)) %>% pull(is_burger))$p.value,
   # Rehnquist Court
         t.test(case_level %>% filter(treatment == 0) %>% mutate(is_rehnquist = ifelse(chief == "Rehnquist", 1, 0)) %>% pull(is_rehnquist),
          case_level %>% filter(treatment == 1) %>% mutate(is_rehnquist = ifelse(chief == "Rehnquist", 1, 0)) %>% pull(is_rehnquist))$p.value,
   # Roberts Court
         t.test(case_level %>% filter(treatment == 0) %>% mutate(is_roberts = ifelse(chief == "Roberts", 1, 0)) %>% pull(is_roberts),
          case_level %>% filter(treatment == 1) %>% mutate(is_roberts = ifelse(chief == "Roberts", 1, 0)) %>% pull(is_roberts))$p.value,
   # On Appeal
            t.test(case_level %>% filter(treatment == 0) %>% pull(on_appeal),
          case_level %>% filter(treatment == 1) %>% pull(on_appeal))$p.value,
      # Federal Court Conflict
            t.test(case_level %>% filter(treatment == 0) %>% pull(conflict_case),
          case_level %>% filter(treatment == 1) %>% pull(conflict_case))$p.value,
      # Polarized Issue Area
            t.test(case_level %>% filter(treatment == 0) %>% pull(polarized_issue),
          case_level %>% filter(treatment == 1) %>% pull(polarized_issue))$p.value,
         # Judicial Review
            t.test(case_level %>% filter(treatment == 0) %>% pull(judicial_review),
          case_level %>% filter(treatment == 1) %>% pull(judicial_review))$p.value,
         # LC alignment
            t.test(case_level %>% filter(treatment == 0) %>% pull(lc_alignment),
          case_level %>% filter(treatment == 1) %>% pull(lc_alignment))$p.value,
            # LC disagreement
            t.test(case_level %>% filter(treatment == 0) %>% pull(lc_disagreement),
          case_level %>% filter(treatment == 1) %>% pull(lc_disagreement))$p.value,
   # LC disagreement (vertical)
               t.test(case_level %>% filter(treatment == 0) %>% pull(lc_vertical_dis),
          case_level %>% filter(treatment == 1) %>% pull(lc_vertical_dis))$p.value,
   # U.S. as party
               t.test(case_level %>% filter(treatment == 0) %>% pull(us_as_party),
          case_level %>% filter(treatment == 1) %>% pull(us_as_party))$p.value,
   # early salience est
               t.test(case_level %>% filter(treatment == 0) %>% pull(early.salience.est),
          case_level %>% filter(treatment == 1) %>% pull(early.salience.est))$p.value
  )

diff_chief %>%
  bind_rows(diff_appeal, diff_conflict_case, diff_polarized_issue, diff_judicial_review, diff_lc_alignment, diff_lc_disagreement, diff_lc_vertical_dis, diff_us_as_party, diff_early.salience.est) %>%
  mutate(`p-value` = p_values) %>%
  xtable(caption = "Variable means for election law and non-election cases",
         label = "descriptive_diff",
         digits = c(0, 0, 2, 2, 3)) %>%
  print(include.rownames = FALSE,
        comment = FALSE)



treatment_by_chief <- scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% filter(treatment_fct == "Election law") %>% count(chief)
# Election law cases: Warren Court
treatment_by_chief %>% filter(chief == "Warren") %>% pull(n)
# Election law cases: Burger Court
treatment_by_chief %>% filter(chief == "Burger") %>% pull(n)
# Election law cases: Rehnquist Court
treatment_by_chief %>% filter(chief == "Rehnquist") %>% pull(n)
# Election law cases: Roberts Court
treatment_by_chief %>% filter(chief == "Roberts") %>% pull(n)
```




```{r three_partisanships, echo = FALSE, fig.cap="\\label{three_partisanships}Predicting Conforming Votes with Three Measures of Partisanship, Non-Election Cases", fig.width = 4, fig.height = 3}

# Revealed preference measure, proportion conforming
control_plot_tbl %>% filter(Measurement == "Revealed Preference") %>% pull(estimate)

control_plot_tbl %>%
  ggplot(aes(x = Measurement, y = `Conforming Vote`, ymin = lower_est, ymax = upper_est)) +
  geom_point() +
  geom_linerange() +
  xlab("Ways of Measuring Party ID") +
  ylim(c(0.55, 0.63)) +
  theme_bw() +
  coord_flip()

```





```{r starting_mods, echo = FALSE, results = "asis"}

# Very simple models

simple_orig <- lrm(conforming_vote ~ treatment, data = scdb, x = TRUE, y = TRUE, maxit = 1000)
simple <- robcov(simple_orig, cluster = scdb$caseId)

s_warr_orig <- lrm(conforming_vote ~ treatment, data = scdb_warren, x = TRUE, y = TRUE, maxit = 1000)
s_warr <- robcov(s_warr_orig, cluster = scdb_warren$caseId)


s_burg_orig <- lrm(conforming_vote ~ treatment, data = scdb_burger, x = TRUE, y = TRUE, maxit = 1000)
s_burg <- robcov(s_burg_orig, cluster = scdb_burger$caseId)

s_rehn_orig <- lrm(conforming_vote ~ treatment, data = scdb_rehnquist, x = TRUE, y = TRUE, maxit = 1000)
s_rehn <- robcov(s_rehn_orig, cluster = scdb_rehnquist$caseId)

s_rob_orig <- lrm(conforming_vote ~ treatment, data = scdb_roberts, x = TRUE, y = TRUE, maxit = 1000)
s_rob <- robcov(s_rob_orig, cluster = scdb_roberts$caseId)

stargazer(simple, s_warr, s_burg, s_rehn, s_rob,
          keep.stat = c("n", "ll"),
          digits = 2,
          label = "reg_simple",
          title = "Logistic regressions predicting partisanship-conforming votes with whether a case falls into the election law category; standard errors clustered by case",
          column.labels = c("All Cases", "Warren", "Burger", "Rehnquist", "Roberts"),
          dep.var.labels = "Conforming vote",
          covariate.labels = "Election Law Case",
          style = "APSR",
          header = FALSE)
```

```{r more_mods, echo = FALSE, results = "asis"}
# Roberts Court, proportion cases on appeal that are in treatment category
scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% filter(on_appeal == 1 & chief == "Roberts") %>% pull(treatment) %>% mean()
# Warren Court, proportion cases on appeal that are in treatment category
scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% filter(on_appeal == 1 & chief == "Warren") %>% pull(treatment) %>% mean()
# Burger Court, proportion cases on appeal that are in treatment category
scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% filter(on_appeal == 1 & chief == "Burger") %>% pull(treatment) %>% mean()
# Rehnquist Court, proportion cases on appeal that are in treatment category
scdb %>% group_by(caseId) %>% slice(1) %>% ungroup() %>% filter(on_appeal == 1 & chief == "Rehnquist") %>% pull(treatment) %>% mean()
# Last term with salience data
scdb %>% filter(! is.na(early.salience.est)) %>% pull(term) %>% max()
# Missing salience, pre-Roberts
scdb %>% filter(is.na(early.salience.est), chief != "Roberts") %>% nrow()

glm_all_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + treatment_fct, data = scdb, x = TRUE, y = TRUE, maxit = 1000)
glm_all <- robcov(glm_all_orig, cluster = scdb$caseId)

glm_warr_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + early.salience.est+ treatment_fct, data = scdb_warren, x = TRUE, y = TRUE, maxit = 1000)
glm_warr <- robcov(glm_warr_orig, cluster = scdb_warren$caseId)

glm_burg_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + early.salience.est+ treatment_fct, data = scdb_burger, x = TRUE, y = TRUE, maxit = 1000)
glm_burg <- robcov(glm_burg_orig, cluster = scdb_burger$caseId)

glm_rehn_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + early.salience.est + treatment_fct, data = scdb_rehnquist, x = TRUE, y = TRUE, maxit = 1000)
glm_rehn <- robcov(glm_rehn_orig, cluster = scdb_rehnquist$caseId)

glm_rob_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review + polarized_issue + us_as_party + treatment_fct, data = scdb_roberts, x = TRUE, y = TRUE, maxit = 1000)
glm_rob <- robcov(glm_rob_orig, scdb_roberts$caseId)


stargazer(glm_all, glm_warr, glm_burg, glm_rehn, glm_rob,
          keep = c("lc_alignment",
                   "on_appeal",
                   "conflict_case",
                   "lc_disagreement",
                   "lc_vertical_dis",
                   "republican_pid",
                   "judicial_review",
                   "polarized_issue",
                   "us_as_party",
                   "early.salience.est",
                   "treatment_fct",
                   "Constant"),
                    keep.stat = c("n", "ll"),
          covariate.labels = c("Republican Justice",
                               "Lower Court Alignment",
                               "On Appeal",
                               "Federal Court Conflict",
                               "Lower Court Disagreement",
                               "LC Disagreement (Vertical)",
                               "Judicial Review",
                               "Civil Rights/Liberties",
                               "U.S. as Party",
                               "Early Salience Estimate",
                               "Election Law Case",
                               "Constant"),
          digits = 2,
          label = "reg",
          title = "Logistic regressions predicting partisanship-conforming votes with whether a case falls into the election law category, with controls; standard errors clustered by case",
          column.labels = c("All Cases", "Warren", "Burger", "Rehnquist", "Roberts"),
          dep.var.labels = "Conforming vote",
          style = "APSR",
          header = FALSE)
```






```{r glms_plot, echo = FALSE, message = FALSE, warning = FALSE, fig.width = 7.5, fig.height = 3.75, out.width = "95%", fig.align = "center", fig.cap = "\\label{glm_preds}Predicted probabilities of conforming votes for election law and non-election cases, by Chief Justice"}

# Predicted probability plots
plot1 <- predict_response(glm_warr, terms = "treatment_fct", margin = "empirical") %>% 
  plot() + ylim(c(0.52, 1)) + ggtitle("Warren") + xlab("") + ylab("Conforming Vote")
plot2 <- predict_response(glm_burg, terms = "treatment_fct", margin = "empirical") %>% 
  plot() + ylim(c(0.52, 1)) + ggtitle("Burger") + xlab("") + ylab("")
plot3 <- predict_response(glm_rehn, terms = "treatment_fct", margin = "empirical") %>% 
  plot() + ylim(c(0.52, 1)) + ggtitle("Rehnquist") + xlab("") + ylab("")
plot4 <- predict_response(glm_rob, terms = "treatment_fct", margin = "empirical") %>%
  plot() + ylim(c(0.52, 1)) + ggtitle("Roberts") + xlab("") + ylab("")

plot1 + plot2 + plot3 + plot4 + plot_layout(nrow = 1)

```


```{r roberts_plot, echo = FALSE, message = FALSE, warning = FALSE, fig.width = 3.75, fig.height = 3.75, fig.align = "center", fig.cap = "\\label{gorsuch}Predicted probabilities of conforming votes for election law and non-election cases, by Roberts Court era (pre- and post-Gorsuch)"}


glm_rob_origALT1 <- lrm(conforming_vote ~ republican_pid + lc_alignment + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review + polarized_issue + us_as_party + treatment_fct, data = scdb_roberts %>% filter(naturalCourt < 1706), x = TRUE, y = TRUE, maxit = 1000)
glm_robALT1 <- robcov(glm_rob_origALT1, scdb_roberts %>% filter(naturalCourt < 1706) %>% pull(caseId))

glm_rob_origALT2 <- lrm(conforming_vote ~ republican_pid + lc_alignment + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review + polarized_issue + us_as_party + treatment_fct, data = scdb_roberts %>% filter(naturalCourt >= 1706), x = TRUE, y = TRUE, maxit = 1000)
glm_robALT2 <- robcov(glm_rob_origALT2, scdb_roberts %>% filter(naturalCourt >= 1706) %>% pull(caseId))

plotROBALT1 <- predict_response(glm_robALT1, terms = "treatment_fct", margin = "empirical") %>% 
  plot() + ylim(c(0.52, 1)) + ggtitle("Pre-Gorsuch") + xlab("") + ylab("Conforming Vote")
plotROBALT2 <- predict_response(glm_robALT2, terms = "treatment_fct", margin = "empirical") %>% 
  plot() + ylim(c(0.52, 1)) + ggtitle("Post-Gorsuch") + xlab("") + ylab("")
plotROBALT1 + plotROBALT2 + plot_layout(nrow = 1)

```



```{r fixed_effects, echo = FALSE, results = "asis"}

allchiefs <-  felm(conforming_vote ~ lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + treatment | term + justiceName | 0 | caseId, data = scdb)

warr <- felm(conforming_vote ~ lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + early.salience.est + treatment | term + justiceName | 0 | caseId, data = scdb_warren)
  
burg <- felm(conforming_vote ~ lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + early.salience.est + treatment | term + justiceName | 0 | caseId, data = scdb_burger)

rehn <- felm(conforming_vote ~ lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + early.salience.est + treatment | term + justiceName | 0 | caseId, data = scdb_rehnquist)

rob <- felm(conforming_vote ~ lc_alignment + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + treatment | term + justiceName | 0 | caseId, data = scdb_roberts)

stargazer(allchiefs, warr, burg, rehn, rob,
          keep = c("ideo_extreme",
                   "lc_alignment",
                   "on_appeal",
                   "conflict_case",
                   "lc_disagreement",
                   "lc_vertical_dis",
                   "republican_pid",
                   "judicial_review",
                   "polarized_issue",
                   "us_as_party",
                   "early.salience.est",
                   "treatment"),
                    covariate.labels = c("Lower Court Alignment",
                               "On Appeal",
                               "Federal Court Conflict",
                               "Lower Court Disagreement",
                               "LC Disagreement (Vertical)",
                               "Judicial Review",
                               "Civil Rights/Liberties",
                               "U.S. as Party",
                               "Early Salience Estimate",
                               "Election Law Case",
                               "Constant"),
                    keep.stat = c("n", "adj.rsq"),
          digits = 2,
          label = "reg_complex",
          title = "Linear fixed effects models predicting partisanship-conforming votes with whether a case falls into the election law category, with controls and justice and term fixed effects; standard errors clustered by case",
          column.labels = c("All Cases", "Warren", "Burger", "Rehnquist", "Roberts"),
          dep.var.labels = "Conforming vote",
          style = "APSR",
          add.lines = list(c("Term Fixed Effects", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark"),
                           c("Justice Fixed Effects", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark", "\\checkmark")),
          header = FALSE)
```



```{r election_calendar, echo = FALSE}

# Interact with year of a presidential election, overall

all2_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + treatment_fct * `Presidential election year`, data = scdb, x = TRUE, y = TRUE, maxit = 1000)
all2 <- robcov(all2_orig, cluster = scdb$caseId)

# Interact with year of a presidential election, by chief

warr2_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review + polarized_issue + us_as_party + early.salience.est+ treatment_fct * `Presidential election year`, data = scdb_warren, x = TRUE, y = TRUE, maxit = 1000)
warr2 <- robcov(warr2_orig, cluster = scdb_warren$caseId)

burg2_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + early.salience.est+ treatment_fct * `Presidential election year`, data = scdb_burger, x = TRUE, y = TRUE, maxit = 1000)
burg2 <- robcov(burg2_orig, cluster = scdb_burger$caseId)

rehn2_orig <- lrm(conforming_vote ~ republican_pid + lc_alignment + on_appeal + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + early.salience.est+ treatment_fct * `Presidential election year`, data = scdb_rehnquist, x = TRUE, y = TRUE, maxit = 1000)
rehn2 <- robcov(rehn2_orig, cluster = scdb_rehnquist$caseId)

rob2_orig <- lrm(conforming_vote ~ republican_pid  + conflict_case + lc_disagreement + lc_vertical_dis + judicial_review  + polarized_issue + us_as_party + treatment_fct * `Presidential election year`, data = scdb_roberts, x = TRUE, y = TRUE, maxit = 1000)
rob2 <- robcov(rob2_orig, cluster = scdb_roberts$caseId)

```


```{r plotpres, echo = FALSE, message = FALSE, warning = FALSE, fig.width = 7.5, fig.height = 3.75, out.width = "95%", fig.align = "center", fig.cap =  "\\label{pres_preds}Predicted probabilities of conforming votes for election law and non-election cases, by Chief Justice and whether a case was decided in a presidential election year"}


plotpres1 <- predict_response(warr2, terms = c("treatment_fct", "Presidential election year"), margin = "empirical") %>% plot(colors = "bw") + ylim(c(0.45, 1)) + ggtitle("Warren") + xlab("") + ylab("Conforming Vote")

plotpres2 <- predict_response(burg2, terms = c("treatment_fct", "Presidential election year"), margin = "empirical") %>% plot(colors = "bw") + ylim(c(0.45, 1)) + ggtitle("Burger") + xlab("") + ylab("")

plotpres3 <- predict_response(rehn2, terms = c("treatment_fct", "Presidential election year"), margin = "empirical") %>% plot(colors = "bw") + ylim(c(0.45, 1)) + ggtitle("Rehnquist") + xlab("") + ylab("")

plotpres4 <- predict_response(rob2, terms = c("treatment_fct", "Presidential election year"), margin = "empirical") %>% plot(colors = "bw") + ylim(c(0.45, 1)) + ggtitle("Roberts") + xlab("") + ylab("")

plotpres1 + plotpres2 + plotpres3 + plotpres4 + plot_layout(nrow = 1, guides = "collect") & theme(legend.position = "bottom")
```

